package meta.common.util;

import java.util.Date;
import java.util.UUID;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

public class JwtUtil {

    /**
     * 创建令牌
     * 
     * @param account 账号
     * @param password 密码
     * @param expiresHours 过期小时数
     * @return 令牌
     */
    public static String buildToken(String account, String password, int expiresHours){
        long nowTime = new Date().getTime();
        long expTime = nowTime + (1000 * 60 * 60 * expiresHours);
        return JWT.create().withJWTId(UUID.randomUUID().toString())
                           .withSubject(account)
                           .withAudience(account)
                           .withIssuedAt(new Date(nowTime))
                           .withExpiresAt(new Date(expTime))
                           .sign(Algorithm.HMAC256(password));
    }
}
